import VueRouter from "vue-router"
import Vue from 'vue'
Vue.use(VueRouter)

// 捕获当前路由与目标路由重复的报错
const VueRouterPush = VueRouter.prototype.push
VueRouter.prototype.push = function push (to) {
  return VueRouterPush.call(this, to).catch(err => err)
}

let router = new VueRouter({
    routes : [
        {
            path: '/',
        },
        {
            path: '/login',
            name: 'Login',
            component: () => import(/* webpackChunkName: "login" */ '@/views/login/index.vue'),
        },
    ]
})

// 定时器
let timer = null

// 路由守卫
router.beforeEach((cur, pre, next) => {
    console.log(cur.path )
    // if(cur.path !== '/login' && cur.path !== '/forgotpwd'){
    //   if(tools.cookie.get('Admin-Token')){
    //     next()
    //   }else{
    //     router.push('/login')
    //   }
    // }else {
    //   next()
    // }
    next()
  })

// 切换页面回到顶部
router.afterEach((prevPage, tartgetPage, next) => {
  let cur,speed = 0
  let isMove = true
  let doc = null

  clearInterval(timer)
  timer = setInterval(() => {
      isMove = true
      doc = document.documentElement || document.body
      cur = doc.scrollTop
      speed = Math.floor((0 - cur) / 8)
      if(cur === 0){
          clearInterval(timer)
      }else{
          doc.scrollTop = cur + speed
      }
  }, 20);

  window.onscroll = () => {
    if(!isMove){
      clearInterval(timer)
    }
    isMove = false
  }

})
export default router

